class Solution:
    def reverse(self, x: int) -> int:
        if x < 0:
            sign = -1
            x = -x
        else:
            sign = 1
        result = 0
        while x != 0:
            digit = x % 10
            result = result * 10 + digit
            x = x // 10
        result = result * sign
        if result < -2**31 or result > 2**31 - 1:
            return 0
        else:
            return result

# 测试示例
print(Solution().reverse(123))
print(Solution().reverse(-123))
print(Solution().reverse(120))
print(Solution().reverse(0))